﻿
var hdnReplaceTextString = $("#hdnReplaceText").val();
var hdnResidentialString = $("#hdnResidential").val();
var hdnBusinessString = $("#hdnBusiness").val();
var hdnSendMailFaiString = $("#hdnSendMailFail").val();
var hdnSendMailNakedString = $("#hdnSendMailNaked").val();
var hdnUpdateAvailabilityString = $("#hdnUpdateAvailability").val();
var hdnUserIDString = $("#hdnUserID").val();
$(function () {

    //On Page Load show hide the Other Panel
   // if ($("#ADSLprovider option:selected").text() == "Other") {
       // $("#divshowhideOther").show();
   // }
   // else {
       // $("#ADSLproviderOther").val('');
       // $("#divshowhideOther").hide();
    //}

    var getServiceAddress = $("#ServiceAddress").val();
    var getServicePhone = $("#ServicePhoneNumber").val();
    //Set value of plan from the Cookie on page load
    var chkSplit = $.cookie("MainCookie");
    if (chkSplit != null) {
        var planSelected = $.cookie("MainCookie").split('|')[0];
    }
    if (planSelected != '') {
        $("#PlanNamesID").val(planSelected);
    }

    //TopBar 
    $("#link_Availability").addClass("current");
    $("#link_Signup").addClass("completed");

    $(".Topstate li").each(function (index) {
        if ($(this).hasClass('completed')) {
            var strhref = $(this).attr('id').split('_')[1];
            var strshowtext = $(this).text();
            var makehref = hdnReplaceTextString;
            var afterreplace = makehref.replace("ReplaceText", strhref);
            $(this).html("<a href=" + afterreplace + ">" + strshowtext + "</a>");
        }
    });

    var chkSplit = $.cookie("MainCookie");
    if (chkSplit != null) {
        var secondchk = $.cookie("MainCookie").split('|')[1];
    }
    var divToBeWorkedOnNumber = "#AjaxPlaceHolderNumber";
    //local
    var webMethodNumber = "/checkData.asmx/NumberBased";
    var planValue = secondchk;
    var userID = hdnUserIDString;
    var parametersNumber = "{'fullnationalnumber':'" + getServicePhone + "','planID':'" + planValue + "'}";

    var TransActRes = hdnResidentialString;
    var TransActBus = hdnBusinessString;
    //Service Qualification: dont check for trans Plans.. 
    if (planValue == TransActRes || planValue == TransActBus || getServicePhone == "") // Trans plans // if no service number then dont check for it.
    { }
    else {
        $("#loaderCenter").show();
        $('#screen').css({ 'display': 'block', opacity: 0.3, 'width': $(document).width(), 'height': $(document).height() });
        $.ajax({
            type: "POST",
            url: webMethodNumber,
            data: parametersNumber,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                $(divToBeWorkedOnNumber).html(msg.d);
                if (msg.d == "0") //No service exists for it. show other plans that are available for this.
                {
                    $("#divshowQualificationFail").show();
                    $("#AjaxPlaceHolderConnection").html("<span style='color:Red;'>Plan not Available in your area, please select below.</span>");
                    $("#loaderCenter").hide();
                    $('#screen').css({ 'display': 'none' });
                }
                else if (msg.d == "-1") //show error message. No service exists for it and send mail.
                {
                    $("#AjaxPlaceHolderConnection").html("<span style='color:Red;'>Automated qualification is currently unavailable for your supplied details.</span>");

                    //send an Ajax request to send mails:
                    var urlInner = hdnSendMailFaiString;
                    var dataInner = "{'userID':'" + userID + "'}"
                    $.ajax({
                        type: "POST",
                        url: urlInner,
                        data: dataInner,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {
                        },
                        error: function (e) {
                        }
                    });
                    //end Request
                    $("#loaderCenter").hide();
                    $('#screen').css({ 'display': 'none' });
                }

                else if (msg.d == "1") {
                    $("#AjaxPlaceHolderConnection").html("<span style='color:Green;'>Plan Available in your area.</span>");
                    $("#loaderCenter").hide();
                    $('#screen').css({ 'display': 'none' });
                }
                else if (msg.d == "2") //service exists and also dsl is already exists.Please must select the existing provider below.
                {
                    $("#AjaxPlaceHolderConnection").html("<span style='color:Green;'>Plan Available in your area.</span>");
                    //$("#divExistingProvider").show();
                    $("#loaderCenter").hide();
                    $('#screen').css({ 'display': 'none' });
                }
                else if (msg.d == "4") //Naked DSL(4)
                {
                    $("#divshowQualificationFail").hide();
                    $("#AjaxPlaceHolderConnection").html("<span style='color:Green;'>Service Qualification is being processed. You can proceed further.</span>");

                    //send an Ajax request to send mails:
                    var urlInner = hdnSendMailNakedString;
                    var dataInner = "{'userID':'" + userID + "'}"
                    $.ajax({
                        type: "POST",
                        url: urlInner,
                        data: dataInner,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {
                        },
                        error: function (e) {
                        }
                    });
                    //end Request
                    $("#loaderCenter").hide();
                    $('#screen').css({ 'display': 'none' });
                }
            },
            error: function (e) {
                $(divToBeWorkedOnNumber).html("0");
                $("#loaderCenter").hide();
                $('#screen').css({ 'display': 'none' });

            }
        });
    }

    //var divToBeWorkedOnAddress= "#AjaxPlaceHolderAddress";
    //local
    // var webMethodAddress = "/checkData.asmx/AdddressBased";

    //var parametersAddress = "{'Address':'" + getServiceAddress + "'}";

    //$.ajax({
    //  type: "POST",
    // url: webMethodAddress,
    // data: parametersAddress,
    // contentType: "application/json; charset=utf-8",
    //dataType: "json",
    // success: function (msg) {
    // $(divToBeWorkedOnAddress).html(msg.d);
    // if(msg.d=="0")
    // {
    // $("#divshowQualificationFail").show();
    //$("#AjaxPlaceHolderConnection").html("<span style='color:Red;'>Plan not Available in your area, please select below.</span>");
    //}
    // },
    // error: function (e) {
    //  $(divToBeWorkedOnAddress).html("0");
    //}
    //});
});
       //End Main 
       //start btnclickNextSubmit
      function btnclickNextSubmit()
          {
                //if this is blank means success.
                //var checkAllValidation='';

                  //qualification is true return true.
                  if($("#divshowQualificationFail").css("display") != 'none' )
                  {
                                  var selectedvalue=$("#PlanNamesID option:selected").text();
                                  if(selectedvalue=="Please select an option")
                                  {
                                   $("#err").html("<ul><li>Please select Plan name</ul></li>");
                                   $('#divErrPlanes').html("Please select Plan name");
                                   $('#divErrPlanes').show();
                                   $("#loaderCenter").hide();
                                   $('#screen').css({ 'display': 'none' });
                                   return false;
                                  }
                                  else
                                  {
                                    $("#err").html("");
                                    $('#divErrPlanes').html("");
                                    return true;
                                  }
                   }


                   //else if($("#divExistingProvider").css("display") != 'none' )
                   //{
                  
                                    //Validation for ADSL Provider start
                                   // if(($("#ADSLprovider").val()=="") || ($("#ADSLprovider").val()=="7"))
                                   // {
                                   // $("#divErrInsideADSLProvider").html("Please select ADSLprovider.");
                                   //$("#divErrInsideADSLProvider").show();
                                   // checkAllValidation=0;
                                   //}
                                   // else
                                   //{
                                   // $("#divErrInsideADSLProvider").html('');
                                   // $("#divErrInsideADSLProvider").hide();
                                   // }
                                    //Validation for ADSL Provider End
                            
                                   // if($("#ADSLprovider option:selected").text()=="Other")
                                   // {
                                   //     if($("#ADSLproviderOther").val()!='')
                                   //     {
                                   //         $("#divErrOther").html('');
                                   //         $("#ADSLproviderOther").removeClass("errorforaccordian");
                                   //     }
                                   //     else
                                   //     {
                                   //     $("#divErrOther").show();
                                   //    $("#divErrOther").html("Please enter Provider Name");
                                   //     $("#ADSLproviderOther").addClass("errorforaccordian");
                                   //    checkAllValidation=0;
                                   //    }
                                   // }
                                   // else
                                   // {
                                   //     $("#divErrOther").html('');
                                   // }

                                    //This will check the final Result
                                    // if(checkAllValidation=="0")
                                       // {
                                          //  $("#loaderCenter").hide();
                                          //  $('#screen').css({ 'display': 'none' });
                                              //   return false;
                                       // }
                                       // else
                                        //{
                                              //  $("#loaderCenter").show();
                                              //  $('#screen').css({ 'display': 'block', opacity: 0.3, 'width': $(document).width(), 'height': $(document).height() });
                                                //Update Provider values in the database:
                                                 //send an Ajax request to send mails:
                                                //var ID = hdnUserIDString;
                                                //var ADSLProvider=$("#ADSLprovider").val();
                                                //var ADSLProviderOther=$("#ADSLproviderOther").val();

                                                //var urlInner = hdnUpdateAvailabilityString;
                                                //var dataInner="{'ID':'" + ID + "','ADSLProvider':'" + ADSLProvider + "','ADSLProviderOther':'" + ADSLProviderOther + "'}"
                                                // var ret=false;
                                                
                                                 //send an ajax request
                                                 // $.ajax({
                                                 //  type: "POST",
                                                 //  url: urlInner,
                                                 //  data: dataInner,
                                                 //  contentType: "application/json; charset=utf-8",
                                                 // dataType: "json",
                                                 // async:false,
                                                 //  success: function (msg) {
                                                 //  },
                                                 //  error: function (data) {
                                                 //  }
                                              // });
                                               //end an ajax request
                                               // return true;
                                           // }
                            
                           
                   // }
                   // else //there is no condition there.
                   // {
                   // return true;
                   // }
          }
          //end btnclickNextSubmit
                $("#PlanNamesID").live("change", function (e) {
             
                     var drp=$("#PlanNamesID option:selected");
                     if(drp.text()!="Please select an option")
                     {
                     
                     //Assign this Id to cookie
                     
                           var chkSplit= $.cookie("MainCookie");
                           if(chkSplit!=null)
                           {
                           var planIDFixed = $.cookie("MainCookie").split('|')[1];
                           var selectedplan = $.cookie("MainCookie").split('|')[2];
                           }
                           $.cookie("MainCookie",null, { path: '/', expires: -5 });
                           var obj =drp.val()+'|'+planIDFixed+'|'+selectedplan;
                           //create new cookie
                            $.cookie("MainCookie",obj, { path: '/', expires: 1 });


                      $("#err").html("");
                      $('#divErrPlanes').html("");
                     }
                 });

                  $("#btnback").live("click", function (e) {
                      $("#loaderCenter").show();
                      $('#screen').css({ 'display': 'block', opacity: 0.3, 'width': $(document).width(), 'height': $(document).height() });
                  });
          
                  // $("#ADSLprovider").live("change", function (e) {
                        // if($("#ADSLprovider option:selected").text()=="Other")
                       //  {
                         // $("#divshowhideOther").show();
                        // }
                         //else
                        // {
                         //$("#ADSLproviderOther").val('');
                        // $("#divshowhideOther").hide();
                        // }
                  //});